package com.kehutong.crm.admin;

import java.util.List;

import org.coraframework.authz.HasPermission;
import org.coraframework.bean.valid.ValidResult;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.util.Objects;

import com.kehutong.crm.entity.Category;

/**
 * 客户分类控制器
 * @author: liukunlong
 * @date: 2021-02-19 15:12
 */
@MvcAction
@ReqMapping("/admin/crm/category")
public class CategoryController {

	@Inject
	private JdbcSession jdbcSession;

	@HasPermission("crm:category:view")
	@ReqMapping("/list")
	public Object list(JSONObject jsonObject) throws Exception {
		return jdbcSession.findPage(Category.class)
				.eq(jsonObject, "id", "companyNo")
	    		.like(jsonObject, "name")
	    		.eq("deleted", false)
	    		.order("createTime", "desc")
	    		.page(jsonObject)
	    		.exe();
	}

	@HasPermission("crm:category:view")
	@ReqMapping("/listAll")
	public Object listAll(JSONObject jsonObject) {
		return jdbcSession.findArray(Category.class)
					.eq(jsonObject, "id", "companyNo")
					.like(jsonObject, "name")
					.eq("deleted", false)
					.order("createTime", "desc")
					.exe();
	}

	@HasPermission("crm:category:view")
	@ReqMapping("/get")
	public Object get(String id) {
		Category category = Pool.get(Category.class, id);
		
		return category;
	}

	@Bean(newInstance=true, copy=true)
	@HasPermission("crm:category:edit")
	@ReqMapping("/save")
	public Object save(Category category, ValidResult result) {
		if (result.hasError()) {
			return Result.error(0, result.getErrorMessage());
		}

		if (isHave(category)) {
			return Result.error(-1, "已存在相同的名称！");
		}

		category.setDeleted(false);
		jdbcSession.insert(category);
		return Result.success();
	}

	@Bean(copy=true)
	@HasPermission("crm:category:edit")
	@ReqMapping("/update")
	public Object update(Category category, ValidResult result) {
		if (result.hasError()) {
			return Result.error(0, result.getErrorMessage());
		}

		Category temp = Pool.get(Category.class, category.getId());
		if (Objects.isNull(temp)) {
			return Result.error(-1, "该名称不存在！");
		}

		if (!category.getName().equals(temp.getName())){
			List<Category> exe = jdbcSession.findArray(Category.class)
									   //.eq("companyNo", category.getCompanyNo())
									   .eq("name", category.getName())
									   .eq("deleted", false).exe();
			if (exe.size() != 0) {
				return Result.error(-1, "该名称已存在！");
			}
		}

		jdbcSession.updateById(category);
		return Result.success();
	}

	@HasPermission("crm:category:edit")
	@ReqMapping("/delete")
	public Object delete(String id) throws Exception {
		Category category = Pool.get(Category.class, id);
		
		if (Objects.nonNull(category)) {
			category.setDeleted(true);
			jdbcSession.updateById(category);
		}
		
		return Result.success();
	}

	private boolean isHave(Category category) {
		List<Category> res = jdbcSession.findArray(Category.class)
									   //.eq("companyNo", category.getCompanyNo())
									   .eq("name", category.getName())
									   .eq("deleted", false)
									   .exe();

		if (Objects.nonEmpty(res)) {
			return true;
		}

		return false;
	}

}
